package com.bbcare.department.plat.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.bbcare.comm.OSSClientUtil;
import com.bbcare.comm.PropertiesUtils;
import com.bbcare.comm.ResultModel;


@Controller
@RequestMapping(value= "/service")
public class UploadController {
	Logger logger = Logger.getLogger(UploadController.class);

	//@Autowired
	//private IDepartmentBasicService deparmentBasicService;

	@ResponseBody
	@RequestMapping("/common/upload")
	public ResultModel<Object> uploadPicture(HttpServletResponse response, HttpServletRequest request) throws Exception{
			logger.info("in uploadPicture ");
			String filePath = request.getParameter("filePath");
			String key = PropertiesUtils.getProperty(filePath);
			String filepath = "";
			CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
	            if (multipartResolver.isMultipart(request)) {
	                MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
	                Iterator<String> iter = multiRequest.getFileNames();
	                while (iter.hasNext()) {
	                    MultipartFile file = multiRequest.getFile(iter.next());
	                    if(!file.isEmpty()){
	                    	filepath = OSSClientUtil.uploadPicture(file,key);//保存图片
	                    }
	             }
	        }

            return new ResultModel<Object>(filepath);
		}

	  	//文件保存到服务器上
	    protected String savePicture(MultipartFile file, HttpServletRequest request)
	            throws IOException, FileNotFoundException {

	    	String ImagePath = request.getSession().getServletContext().getRealPath("/static/img");
	        File targetfile = new File(ImagePath, file.getOriginalFilename());
	        Date date = new Date(System.currentTimeMillis());
	        if(targetfile.exists()){
	            targetfile = new File(ImagePath, file.getOriginalFilename() + date.toString());
	        }
	        InputStream ins = file.getInputStream();
	        FileOutputStream fos = new FileOutputStream(targetfile);

	        byte b[] = new byte[1024];
	        int temp = 0;

	        while((temp = ins.read(b)) != -1){
	            fos.write(b, 0, temp);
	        }

	        fos.close();
	        ins.close();
	        String str = targetfile.getName();
	        return str;
	    }
}
